using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Web;
using System.Web.Services;
using Dimok.Aspx.Manager.Lib;
using Dimok.Exceptions;
using Data.DataSources;

namespace Dimok.Aspx.Manager.Imaging{
	public class Imaging : Dimok.Images.WS.Imaging {
		[WebMethod]
		public void Batch_UploadImage(int batchID, string fileName) {
			try {
				fileName = Dimok.Images.ImageFiles.FilePathFull(fileName);
				byte[] imageFrom = File.ReadAllBytes(fileName);
				if (Dimok.Images.Enums.GetContentType(imageFrom) != Dimok.Images.Enums.CONTENT_TYPES_PDF)
					imageFrom = Dimok.ABCPdf.TiffToPDF(imageFrom);
				DataView dv = new PurchaseBatchesDataSource().SelectEx(batchID);
				byte[] imageTo = (byte[])(dv.Count > 0 && dv[0]["BatchImages"] != null && dv[0]["BatchImages"] != DBNull.Value ? dv[0]["BatchImages"] : null);
				imageTo = imageTo != null ? Dimok.Documents.PDF.CombinePDF(new byte[][] { imageTo, imageFrom }) : imageFrom;
				new PurchaseBatchesDataSource().UpdateField(new OrderedDictionaryEx("ID", batchID), "BatchImages", imageTo,false);
			} catch (Exception exc) { throw new ASPException(exc); }
		}
	
	}
}
